package gaml.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import gaml.core.Gaml;
import gaml.error.GamlException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;


public class GamlTest {
	
	@Test
	public void testSerializeEmptyMap () throws GamlException {
		Map <String, String> map = new HashMap <String, String> ();
		
		System.out.println (new Gaml ().serialize (map));
	}
	
	@Test
	public void testSerializeTClassOfTStringBuilder () throws GamlException {
		StringBuilder sb = new StringBuilder ();
		Gaml gaml = new Gaml ();
		ArrayList <Integer> list = new ArrayList <Integer> ();
		list.add (10);
		list.add (20);
		Bar bar = new Bar ("Name", 10, list, "Bar Name");
		
		gaml.serialize (bar, Bar.class, sb);

		assertTrue (sb.toString ().contains ("? > name\n: > Name\n"));
		assertTrue (sb.toString ().contains ("? > id\n: > 10\n"));
		assertTrue (sb.toString ().contains ("? > barname\n: > Bar Name\n"));
		
//		System.out.println (sb.toString ());
	}

	@Test
	public void testDeserializeTClassOfTString () throws GamlException {
//		String yaml = "? > m\n:\n\n	? > key1\n	: > val1\n	? > key2\n	: > val2\n? > id\n: > 290\n? > sub\n:\n\n	? > sm\n	:\n\n		? > key1\n		: > 1\n	? > subname\n	: > Sub Name 1\n	? > list\n	:\n\n		- > 10\n		- > 20\n? > name\n: > Name 1\n? > barname\n: > Bar Name 1\n";
		StringBuilder sb = new StringBuilder ();
		Gaml gaml = new Gaml ();
		ArrayList <Integer> list = new ArrayList <Integer> ();
		list.add (10);
		list.add (20);
		Bar bar = new Bar ("Name 1", 290, list, "Bar Name 1");
		
		gaml.serialize (bar, Bar.class, sb);
		String yaml = sb.toString ();
		
		gaml = new Gaml ();
		
		bar = new Bar ("", 0, new ArrayList <Integer> (), "");
		gaml.restore (bar , Bar.class, yaml);

		assertEquals ("Name 1", bar.getName ());
		assertEquals (290, bar.getId ());
		assertEquals ("Bar Name 1", bar.getBarName ());
	}

	@Test
	public void testSerializeTClassOfT () throws GamlException {
		Gaml gaml = new Gaml ();
		Bar bar = new Bar ("Name", 10, new ArrayList <Integer> (), "Bar Name");
		
		assertTrue (gaml.serialize (bar, Bar.class).contains ("? > name\n: > Name\n"));
		assertTrue (gaml.serialize (bar, Bar.class).contains ("? > id\n: > 10\n"));
		assertTrue (gaml.serialize (bar, Bar.class).contains ("? > barname\n: > Bar Name\n"));
	}

	@Test
	public void testDeserializeStringClassOfT () throws GamlException {
		String yaml = "? > m\n:\n\n	? !java.lang.String > key1\n	: !java.lang.String > val1\n	? !java.lang.String > key2\n	: !java.lang.String > val2\n? > id\n: > 290\n? > sub\n:\n\n	? > sm\n	:\n\n		? !java.lang.String > key1\n		: !java.lang.Integer > 1\n	? > subname\n	: > Sub Name\n	? > list\n	:\n\n		- !java.lang.Integer > 10\n		- !java.lang.Integer > 20\n? > name\n: > Name 1\n? > barname\n: > Bar Name 1\n";
		Gaml gaml = new Gaml ();
		
		Bar bar = gaml.restore (yaml, Bar.class);
		
		assertEquals ("Name 1", bar.getName ());
		assertEquals (290, bar.getId ());
		assertEquals ("Bar Name 1", bar.getBarName ());
	}
}



class Foo {
	String name;
	int id;
	SubFoo sub = null;
	HashMap <String, String> m = new HashMap <String, String> ();

	public Foo (String name, int id, ArrayList <Integer> list) {
		this.name = name;
		this.id = id;
		this.m.put ("key1", "val1");
		this.m.put ("key2", "val2");
		
		this.sub = new SubFoo (list);
	}

	public String getName () { return name; }
	public int getId () { return id; }
}

class SubFoo {
	ArrayList <Integer> list = new ArrayList <Integer> ();
	HashMap <String, Integer> sm = new HashMap <String, Integer> ();
	String subname = "Sub Name";
	
	public SubFoo (ArrayList <Integer> list) { this.list = list; sm.put ("key1", 1); }
}

class Bar extends Foo {
	String barname;
	
	public Bar (String name, int id, ArrayList <Integer> list, String barname) {
		super (name, id, list);
		this.barname = barname;
	}
	
	public String getBarName () { return barname; }
}